#!/usr/bin/Rscript
# Begin plot_metrics
########
#$# creator : Devon Smith
#%# email: smithde@oclc.org
#$# created : 2010-06-14
#$# title : plot_metrics
#$# description : Create graphs, ploting tag frequency as bars, then precision,
#__# recall, and f-measure plotted as points over the bars.
#$# usage: plot_metrics data-file png-file graph-title
########
args <- commandArgs(TRUE)
data_file <- args[1]
graph_file <- args[2]
graph_title <- args[3]
########

colors <- list( fmeasure="green", precision="blue", recall="red" )
markers <- list( fmeasure="f", precision="p", recall="r" )

metrics <- read.csv(file=data_file, header=T)

# Create a PNG instead of default PDF
png(file=graph_file, width = 400, height = 400)

par(xpd=T, mar=par()$mar+c(0,0,0,7))
# plot the tag counts as bars, along the left axis
# Set the top of the left axis to (largest of the tag frequencies) * 1.05
bar_centers <- barplot(metrics$frequency, names.arg=metrics$label, ylim=c(0, max(metrics$frequency)*1.05) )

par(new=T)
# Adjustment to put the points ~middle of the bars. Works for 4, would need to be adjusted for not 4.
# Needed because the new plot (for points) is not at all lined up with the bar plot.
# Would be best to calculate these, didn't have time to find a good formula.
bar_centers <- c(1.3, 2.1, 2.9, 3.7)

plot(x=bar_centers, type="n", xlab="", ylab="", ylim=c(0,100), axes=F)
points(x=bar_centers, metrics$fmeasure, col=colors$fmeasure, type="p", pch=markers$fmeasure)
points(x=bar_centers, metrics$precision, col=colors$precision, type="p", pch=markers$precision)
points(x=bar_centers, metrics$recall, col=colors$recall, type="p", pch=markers$recall)

# Create the right axis for metrics
axis(4, at=c(0, 20,40,60,80,100) )

# Create a title
title(main=graph_title, col.main="black", font.main=1)

# Create a legend
legend(4.6, 75, c("Precision","Recall","F-Measure"),
	col=c(colors$precision, colors$recall, colors$fmeasure),
	pch=c(markers$precision, markers$recall, markers$fmeasure));

q(status=0)
########
# End plot_metrics
# vim:ts=4:ft=r
